package state.person;

import java.io.Serializable;
import java.util.ArrayList;

import state.group.events.BaseEvent;
import state.group.events.EventCollection;
import state.group.events.EventType;
import state.person.events.BasePersonEvent;
import state.person.events.GoingInEvent;
import state.person.events.PersonRegisterEvent;

public class PersonDispatcherState implements Serializable {
    
    private static final long serialVersionUID = -2378352283449180723L;

    public ArrayList<Person> activePeople = new ArrayList<Person>();
    
    public ArrayList<Person> deactivatedPeople = new ArrayList<Person>();
    
    public void updateUsingEvents(EventCollection events)
    {
        for (BaseEvent e : events)
        {
            if (e instanceof BasePersonEvent)
            {
                Person p = null;
                String compName = ((BasePersonEvent) e).getPersonId().toString();
                
                synchronized(activePeople)
                {
                    for (Person sp : activePeople)
                    {
                        if (sp.getName().equals(compName))
                        {
                            p = sp;
                            break;
                        }
                    }
                }
                
                if (p == null)
                {
                    if (e.getEventType() == EventType.PersonRegisterEvent)
                    {
                        p = ((PersonRegisterEvent) e).getPerson();
                        activePeople.add(p);
                    }
                    
                    continue;
                }
                
                switch (e.getEventType())
                {
                    case DestinationEvent:
                        p.setState(PersonState.atDestination);
                        synchronized(activePeople)
                        {
                            activePeople.remove(p);
                        }
                        synchronized(deactivatedPeople)
                        {
                            deactivatedPeople.add(p);
                        }
                        break;
                    case GoingOutEvent:
                        p.setState(PersonState.goingOut);
                        p.setCurrentElevatorId((byte) 0);
                        break;
                    case InsideEvent:
                        p.setState(PersonState.inside);
                        break;
                    case GoingInEvent:
                        p.setState(PersonState.goingIn);
                        byte eleId = ((GoingInEvent) e).getElevatorId();
                        p.setCurrentElevatorId(eleId);
                        break;
                    case WaitingEvent:
                        p.setState(PersonState.waiting);
                        break;
                }
            }
        }
    }
}
